﻿package displays.Sprites 
{
	import debugs.Debug;
	import fl.video.FLVPlayback;
	import flash.display.Sprite;
	import flash.net.NetConnection;
    import flash.net.NetStream;
	import flash.events.*;
	import infos.LRCitem;
	import net.netvars;
	import fl.video.MetadataEvent;
	import fl.video.VideoEvent;
	
	/**
	 * ...
	 * @author keys_star@163.com
	 */
	public class Ivideo extends Sprite
	{
		public var IV:FLVPlayback
		private var connection:NetConnection;
		public var time:Number = 0;
		private var flvURL:String;
		public var stream:NetStream;
		private var point:Array;
		public var cuePoint:Array;
		public function Ivideo() 
		{
			IV.addEventListener(MetadataEvent.CUE_POINT, cp_listener); 
		}
		public function start():void {
			var lrcitem:LRCitem = new LRCitem;
			cuePoint = lrcitem.getAllTime();
			
			var et:Number = Number(netvars.getVar("EndTime"));
			cuePoint.push(et);
			Debug.send(this + ".timeItem: " + cuePoint.join("]["));
			var videoPath = netvars.getVar("flvURL");
			var StartTime = Number(netvars.getVar("StartTime"));
            IV.source = videoPath;
			IV.play();
			IV.seek(StartTime);
			//IV.addEventListener(VideoEvent.PLAYHEAD_UPDATE, playTime);
			addASCuePoint(cuePoint);
        }
		private function playTime(e:VideoEvent):void {
			var t:Number = e.playheadTime;
			Debug.send(this + ".headTime: " + t);
		}
		public function addASCuePoint(t:Array):void {
			try{
				removeASCuePoint();
			}catch (e:Error) {
				Debug.send(this + ".noCuePoint.err");
			}
			point = t.concat();
			for (var i = 0; i < t.length; i++ ) {
				try{
					IV.addASCuePoint(t[i],"p"+i);
				}catch (e:Error) {
					Debug.send(this + ".addCuePoint.err");
				}
			}
		}
		private function removeASCuePoint():void {
			for (var i = 0; i < point.length; i++ ) {
				try{
					IV.removeASCuePoint(point[i]);
				}catch (e:Error) {
					Debug.send(this + ".noCuePoint.");
				}
			}
		}
		private function cp_listener(eventObject:MetadataEvent):void { 
			//if (eventObject.info.name == "ASpt1") { 
			Debug.sendObj(this, eventObject.info);
			var et:Number = Number(netvars.getVar("EndTime"));
			//Debug.send(this + ".allStop"+et);
			if (eventObject.info.time == et) {
				IV.stop();
				Debug.send(this + ".ivStop");
				dispatchEvent(new Event(Event.COMPLETE));
			}else{
				dispatchEvent(new Event(Event.CHANGE));
			}
		}
		private function netStatusHandler(event:NetStatusEvent):void {
			Debug.send(this + ".code: " + event.info.code);
            switch (event.info.code) {
                case "NetConnection.Connect.Success":
                    break;
                case "NetStream.Play.StreamNotFound":
                    trace("Unable to locate video: " + flvURL);
                    break;
            }
        }
		public function pause():void {
			IV.pause();
		}
		public function repause():void {
			IV.play();
		}
	}
}